<template>
  <div>
    <slot name="header" />
    <el-table
      :data="showTable"
      :border="true"
      :max-height="maxHeight"
      :height="height"
      stripe
      highlight-current-row
      style="width: 100%">
      <el-table-column
        v-for="(column, columnIndex) in data.options"
        :key="columnIndex"
        :prop="column.prop"
        :label="$t(column.label)"
        :min-width="column.minWidth || column.width || 150"
        align="center">
        <template slot-scope="scope" :scope="newSlotScope ? 'scope' : false ">
          <span v-if="column.slotName">
            <slot :name="column.slotName" :row="scope.row" :$index="scope.$index" />
          </span>
          <span v-else-if="column.filter">
            {{ Vue.filter(column['filter'])(scope.row[column.prop]) }}
          </span>
          <span v-else-if="column.render">
            {{ column.render(scope.row) }}
          </span>
          <span v-else-if="column.formatter">
            {{ column.formatter(scope.row, scope.column, scope.row[column.prop], scope.$index) }}
          </span>
          <span v-else>
            {{ scope.row[column.prop] }}
          </span>
        </template>
      </el-table-column>
      <slot name="append" />
    </el-table>
    <el-pagination
      v-show="data.data && data.data.length && pagination > 0"
      :total="total"
      :page-size="pageSize"
      :current-page.sync="currentPage"
      background
      style="padding:20px 0 0;text-align:right ;"
      layout="prev, pager, next"/>
  </div>
</template>

<script>
import Vue from 'vue'
export default {
  name: '',
  props: {
    'data': {
      type: Object,
      default: {}
    },
    'pagination': {
      type: Boolean,
      default: true
    },
    height: [String, Number],
    maxHeight: [String, Number]
  },
  data() {
    return {
      Vue,
      total: 20,
      currentPage: 1,
      pageSize: 20
    }
  },
  computed: {
    showTable() {
      return this.tablePagination(this.data.data)
    }
  },
  methods: {
    tablePagination(data) {
      if (!data) return []
      let array = []
      let startNum = 0
      let endNum = 0
      this.total = data.length
      startNum = (this.currentPage - 1) * this.pageSize
      if (this.currentPage * this.pageSize < this.total) {
        endNum = this.currentPage * this.pageSize
      } else {
        endNum = this.total
      }
      array = data.slice(startNum, endNum)
      return array
    }
  }
}
</script>
